﻿using HandyControl.Controls;
using HandyControl.Data;
using KinTN_WMS_CL.Model;
using KinTN_WMS_CL.Views;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace KinTN_WMS_CL.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {

        private readonly IRegionManager _regionManager;
        private IRegionNavigationJournal regionNavigationJournal;
        private readonly IEventAggregator _eventAggregator;
        public static readonly string GrowlScriptToken = nameof(GrowlScriptToken);
        public ICommand CloseCommand { get; }
        public ICommand MinimizeCommand { get; }
        public ICommand MaximizeCommand { get; }
        public DelegateCommand<string> OpenViewCommand { get;  set; }
        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {

            LoadMenuItems();
            _regionManager = regionManager;
            OpenViewCommand = new DelegateCommand<string>(OpenView);
            CloseCommand = new DelegateCommand(OnClose);
            MinimizeCommand = new DelegateCommand(OnMinimize);
            MaximizeCommand = new DelegateCommand(OnMaximize);
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<NotificationEvent>().Subscribe(ShowNotification);
        }

        public ObservableCollection<MenuItemModel> MenuItems { get; set; } = new ObservableCollection<MenuItemModel>();
        private object _currentContent;

        public object CurrentContent
        {
            get => _currentContent;
            set => SetProperty(ref _currentContent, value);
        }

        public void ShowNotification(Message message)
        {
            GrowlInfo growlInfo = new GrowlInfo()
            {
                Message = message.info,
                WaitTime = 2
            };

            switch (message.type)
            {
                case MessageEnum.Info:
                    Growl.Info(growlInfo);
                    break;
                case MessageEnum.Success:
                    Growl.Success(growlInfo);
                    break;
                case MessageEnum.Warning:
                    Growl.Warning(growlInfo);
                    break;
                case MessageEnum.Error:
                    Growl.Error(growlInfo);
                    break;
                default:
                    break;
            }

        }

        private bool _isMenuExpanded = true;
        public bool IsMenuExpanded
        {
            get => _isMenuExpanded;
            set => SetProperty(ref _isMenuExpanded, value);
        }

        private void LoadMenuItems()
        {
            var homeItem = new MenuItemModel { Title = "首页", Icon = "\\Assets\\Images\\home.png", Name = "Home" };
            var workItem = new MenuItemModel { Title = "作业管理", Icon = "\\Assets\\Images\\job.png" };
            workItem.SubItems.Add(new MenuItemModel { Title = "入库单管理", Icon = "\\Assets\\Images\\In.png", Name = "In" });
            workItem.SubItems.Add(new MenuItemModel { Title = "出库单管理", Icon = "\\Assets\\Images\\Out.png", Name = "Out" });
            workItem.SubItems.Add(new MenuItemModel { Title = "退库单管理", Icon = "\\Assets\\Images\\refund.png", Name = "Refund" });
            workItem.SubItems.Add(new MenuItemModel { Title = "任务单管理", Icon = "\\Assets\\Images\\Quest.png", Name = "Quest" });
            var settingsItem = new MenuItemModel { Title = "基础配置", Icon = "\\Assets\\Images\\Basics.png" };
            settingsItem.SubItems.Add(new MenuItemModel { Title = "料仓管理", Icon = "\\Assets\\Images\\StockBin.png", Name = "StockBin" });
            settingsItem.SubItems.Add(new MenuItemModel { Title = "AGV管理", Icon = "\\Assets\\Images\\AGV.png", Name = "AGV" });
            settingsItem.SubItems.Add(new MenuItemModel { Title = "公共管理", Icon = "\\Assets\\Images\\Commonality.png", Name = "Commonality" });
            settingsItem.SubItems.Add(new MenuItemModel { Title = "接口信息管理", Icon = "\\Assets\\Images\\Commonality.png", Name = "Commonality" });
            var LogsItem = new MenuItemModel { Title = "日志", Icon = "\\Assets\\Images\\log.png" };
            LogsItem.SubItems.Add(new MenuItemModel { Title = "错误日志", Icon = "\\Assets\\Images\\error.png", Name = "Error" });
            LogsItem.SubItems.Add(new MenuItemModel { Title = "接口日志", Icon = "\\Assets\\Images\\Interface.png", Name = "Interface" });
            MenuItems.Add(homeItem);
            MenuItems.Add(workItem);
            MenuItems.Add(settingsItem);
            MenuItems.Add(LogsItem);

            // 设置默认的内容
            CurrentContent = homeItem;
        }

        public void OnMenuItemSelected(MenuItemModel item)
        {
            CurrentContent = item; // 更新主内容区域
        }


        private void OpenView(string obj)
        {
            if (!string.IsNullOrEmpty(obj))
            {
                _regionManager.Regions["MainRegion"].RequestNavigate(obj);
            }

            //_regionManager.Regions["MainRegion"].RequestNavigate(obj, callback =>
            //{
            //    if (callback.Result != null)
            //        regionNavigationJournal = callback.Context.NavigationService.Journal;
            //});
        }

        private void OnClose()
        {
            Application.Current.MainWindow.Close();
        }

        private void OnMinimize()
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;
        }

        private void OnMaximize()
        {
            var window = Application.Current.MainWindow;
            if (window.WindowState == WindowState.Maximized)
                window.WindowState = WindowState.Normal;
            else
                window.WindowState = WindowState.Maximized;
        }
    }
}
